package com.shrcl.engineerprocess.domain.impl;

import com.shrcl.engineerprocess.domain.EngineerLevel;
import com.shrcl.engineerprocess.domain.TSCFinalCost;
import com.shrcl.engineerprocess.domain.TSCManCostCalc;
import com.shrcl.engineerprocess.domain.TSCManCostCalcItem;
import com.shrcl.engineerprocess.domain.TSCWorkLoadType;
import com.shrcl.engineerprocess.domain.impl.TSCFinalCostImpl;
import com.shrcl.engineerprocess.domain.impl.TSCManCostCalcItemImpl;
import com.shrcl.engineerprocess.domain.proxy.TSCManCostCalcProxy;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("TSCMANCOSTCALC_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "tscmancostcalc")
public class TSCManCostCalcImpl extends BaseModelObject implements TSCManCostCalc {
  public TSCManCostCalcImpl() {
    super();
  }
  
  @OneToMany(targetEntity = TSCManCostCalcItemImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "manCostCalc")
  private Collection<TSCManCostCalcItem> manCostCalcItem = new java.util.ArrayList<com.shrcl.engineerprocess.domain.TSCManCostCalcItem>();;
  
  public Collection<TSCManCostCalcItem> getManCostCalcItem() {
    return this.manCostCalcItem;
  }
  
  public TSCManCostCalcItem createAndAddtoManCostCalcItem() {
    getManCostCalcItem().size();
    com.shrcl.engineerprocess.domain.TSCManCostCalcItem tSCManCostCalcItem = new com.shrcl.engineerprocess.domain.impl.TSCManCostCalcItemImpl(this);
    getObjectFactory().create(tSCManCostCalcItem);
    getManCostCalcItem().add(tSCManCostCalcItem);
    return tSCManCostCalcItem;
    
  }
  
  public TSCManCostCalcItem createAndAddtoManCostCalcItem(final Procedure1<TSCManCostCalcItem> updater) {
    getManCostCalcItem().size();
    com.shrcl.engineerprocess.domain.TSCManCostCalcItem tSCManCostCalcItem = new com.shrcl.engineerprocess.domain.impl.TSCManCostCalcItemImpl(this);
    getObjectFactory().create(tSCManCostCalcItem);
    getManCostCalcItem().add(tSCManCostCalcItem);
    return tSCManCostCalcItem;
    
  }
  
  public TSCManCostCalc removeAllManCostCalcItem() {
    for(TSCManCostCalcItem obj : this.manCostCalcItem){
    	getObjectFactory().delete(obj);
    }
    this.manCostCalcItem.clear();
    return this;	
    
  }
  
  public TSCManCostCalc removeFromManCostCalcItem(final TSCManCostCalcItem tSCManCostCalcItem) {
    this.manCostCalcItem.remove(tSCManCostCalcItem);
    getObjectFactory().delete(tSCManCostCalcItem);
    return this;
    
  }
  
  private String name;
  
  public String getName() {
    return this.name;
  }
  
  public TSCManCostCalc setName(final String name) {
    this.name = name;
    return this;			
    
  }
  
  @OneToMany(targetEntity = TSCFinalCostImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "manCostCalc")
  private Collection<TSCFinalCost> finalCostEst = new java.util.ArrayList<com.shrcl.engineerprocess.domain.TSCFinalCost>();;
  
  public Collection<TSCFinalCost> getFinalCostEst() {
    return this.finalCostEst;
  }
  
  public TSCFinalCost createAndAddtoFinalCostEst() {
    getFinalCostEst().size();
    com.shrcl.engineerprocess.domain.TSCFinalCost tSCFinalCost = new com.shrcl.engineerprocess.domain.impl.TSCFinalCostImpl(this);
    getObjectFactory().create(tSCFinalCost);
    getFinalCostEst().add(tSCFinalCost);
    return tSCFinalCost;
    
  }
  
  public TSCFinalCost createAndAddtoFinalCostEst(final Procedure1<TSCFinalCost> updater) {
    getFinalCostEst().size();
    com.shrcl.engineerprocess.domain.TSCFinalCost tSCFinalCost = new com.shrcl.engineerprocess.domain.impl.TSCFinalCostImpl(this);
    getObjectFactory().create(tSCFinalCost);
    getFinalCostEst().add(tSCFinalCost);
    return tSCFinalCost;
    
  }
  
  public TSCManCostCalc removeAllFinalCostEst() {
    for(TSCFinalCost obj : this.finalCostEst){
    	getObjectFactory().delete(obj);
    }
    this.finalCostEst.clear();
    return this;	
    
  }
  
  public TSCManCostCalc removeFromFinalCostEst(final TSCFinalCost tSCFinalCost) {
    this.finalCostEst.remove(tSCFinalCost);
    getObjectFactory().delete(tSCFinalCost);
    return this;
    
  }
  
  public String getEntityName() {
    return this.name;
  }
  
  public void addCostEst(final Procedure1<? super TSCFinalCost> init) {
    TSCFinalCost _createAndAddtoFinalCostEst = this.createAndAddtoFinalCostEst();
    final Procedure1<TSCFinalCost> _function = new Procedure1<TSCFinalCost>() {
        public void apply(final TSCFinalCost it) {
          init.apply(it);
        }
      };
    ObjectExtensions.<TSCFinalCost>operator_doubleArrow(_createAndAddtoFinalCostEst, _function);
  }
  
  public void init() {IGenericQuery<TSCWorkLoadType> query=getObjectFactory().createGenericQuery(TSCWorkLoadType.class,"select e from com.shrcl.engineerprocess.domain.impl.TSCWorkLoadTypeImpl as e   where  e.active = 1      ");
    Collection<TSCWorkLoadType> _list = query.list();
    final Procedure1<TSCWorkLoadType> _function = new Procedure1<TSCWorkLoadType>() {
        public void apply(final TSCWorkLoadType tp) {
          TSCManCostCalcItem _createAndAddtoManCostCalcItem = TSCManCostCalcImpl.this.createAndAddtoManCostCalcItem();
          final Procedure1<TSCManCostCalcItem> _function = new Procedure1<TSCManCostCalcItem>() {
              public void apply(final TSCManCostCalcItem it) {
                InputOutput.<String>println("=========>create TSCManCostCalcItem");
                it.setWorkLoadType(tp);
              }
            };
          ObjectExtensions.<TSCManCostCalcItem>operator_doubleArrow(_createAndAddtoManCostCalcItem, _function);
        }
      };
    IterableExtensions.<TSCWorkLoadType>forEach(_list, _function);IGenericQuery<EngineerLevel> query_1=getObjectFactory().createGenericQuery(EngineerLevel.class,"select e from com.shrcl.engineerprocess.domain.impl.EngineerLevelImpl as e   where  e.active = 1      ");
    Collection<EngineerLevel> _list_1 = query_1.list();
    final Procedure1<EngineerLevel> _function_1 = new Procedure1<EngineerLevel>() {
        public void apply(final EngineerLevel level) {
          TSCFinalCost _createAndAddtoFinalCostEst = TSCManCostCalcImpl.this.createAndAddtoFinalCostEst();
          final Procedure1<TSCFinalCost> _function = new Procedure1<TSCFinalCost>() {
              public void apply(final TSCFinalCost it) {
                it.setEngineerLevel(level);
                it.setCost(Double.valueOf(0d));
              }
            };
          ObjectExtensions.<TSCFinalCost>operator_doubleArrow(_createAndAddtoFinalCostEst, _function);
        }
      };
    IterableExtensions.<EngineerLevel>forEach(_list_1, _function_1);
  }
  
  public static TSCManCostCalc create(final IObjectFactory objectFactory) {
    com.shrcl.engineerprocess.domain.TSCManCostCalc tSCManCostCalc = new com.shrcl.engineerprocess.domain.impl.TSCManCostCalcImpl(
    );
    objectFactory.create(tSCManCostCalc);
    return tSCManCostCalc;			
    
  }
  
  public static TSCManCostCalc createTSCManCostCalcImpl(final IObjectFactory objectFactory, final Procedure1<TSCManCostCalc> init) {
    com.shrcl.engineerprocess.domain.TSCManCostCalc tSCManCostCalc = new com.shrcl.engineerprocess.domain.impl.TSCManCostCalcImpl(
    );
    objectFactory.create(tSCManCostCalc);
    init.apply(tSCManCostCalc);
    
    return tSCManCostCalc;			
    
  }
  
  public TSCManCostCalcProxy toProxy() {
    com.shrcl.engineerprocess.domain.proxy.TSCManCostCalcProxy proxy = new com.shrcl.engineerprocess.domain.proxy.TSCManCostCalcProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
